const { getPool } = require('../../../config/db')

const HanziPinyin = {
  async getAllHanziPinyins() {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM hanzi_pinyin')
    return rows
  },

  async getHanziPinyinById(id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM hanzi_pinyin WHERE id = ?', [id])
    return rows[0]
  },

  async createHanziPinyin(pinyinData) {
    const mysqlPool = await getPool()
    const [result] = await mysqlPool.execute(
      'INSERT INTO hanzi_pinyin (hanzi_id, initial, pinyin, tone, sort_order, audio_path) VALUES (?, ?, ?, ?, ?, ?)',
      [pinyinData.hanzi_id, pinyinData.initial, pinyinData.pinyin, pinyinData.tone, pinyinData.sort_order, pinyinData.audio_path]
    )
    return result.insertId
  },

  async updateHanziPinyin(id, pinyinData) {
    const mysqlPool = await getPool()
    await mysqlPool.execute(
      'UPDATE hanzi_pinyin SET hanzi_id=?, initial=?, pinyin=?, tone=?, sort_order=?, audio_path=? WHERE id=?',
      [pinyinData.hanzi_id, pinyinData.initial, pinyinData.pinyin, pinyinData.tone, pinyinData.sort_order, pinyinData.audio_path, id]
    )
  },

  async deleteHanziPinyin(id) {
    const mysqlPool = await getPool()
    await mysqlPool.execute('DELETE FROM hanzi_pinyin WHERE id = ?', [id])
  },
}

module.exports = HanziPinyin